<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\MissionTemplateCacheEnum;
use App\Enums\OpenShop\MissionTemplateEnum;
use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class OpenShopOrganizationRegionMappingModel extends BaseModel
{
    use KfSoftDeletes;
    protected $table = 'open_shop_organization_region_mapping';

    public $timestamps = false;

    /**
     * @return HasOne
     */
    public function organizationManageRel(): HasOne
    {
        return $this->hasOne(OpenShopOrganizationManageModel::class, "id", "manageId");
    }

    /**
     * 获取公司营运的所有的provinceCode
     * @return array
     */
    public static function getTotalProvinceCode(): array
    {
        return self::provinceCodeNames()->keys()->toArray();
    }

    public static function getProvinceName($provinceCode): string
    {
        return self::provinceCodeNames()[$provinceCode] ?? '';
    }

    /**
     * 营业的省份code, name
     * @return array
     */
    public static function provinceCodeNames(): Collection
    {
        return Cache::remember(MissionTemplateCacheEnum::getProvinceCodeNames(), 600, function () {
            return self::query()->pluck('provinceName', 'provinceCode');
        });
    }

    /**
     * 通过组织主键ID查询省区
     * @param array $manageId
     * @return array
     */
    public static function getRegionInfo(array $manageId): array
    {
        return self::query()
            ->whereIn('manageId', $manageId)
            ->get()
            ->toArray();
    }
}
